gen pvote16 = V162062x
replace pvote16 = . if pvote16 < 0
replace pvote16 = . if pvote16 > 2
replace pvote16 = pvote16 - 1
label var pvote16 "1 = R voted for Donald Trump; 0 = R voted for Hillary Clinton"
gen immbadecon = V162268
replace immbadecon = . if immbadecon < 0
replace immbadecon = immbadecon - 1
label var immbadecon "4 = R strongly agrees that immigration is bad for USA economy; 0 = otherwise"
gen immculture = V162269
replace immculture = . if immculture < 0
replace immculture = abs(immculture - 5)
label var immculture "4 = R strongly agrees that American culture is generally harmed by immigration"
gen immcrime = V162270
replace immcrime = . if immcrime < 0
replace immcrime = abs(immcrime - 5)
label var immcrime "4 = R strongly agrees that immigrants increase crime rates in the USA"
factor immbadecon immculture immcrime, pcf
predict fimmthreat
label var fimmthreat "Factor score for immculture, immcrime, immbadecon (Eigelvalue = 2.12)"
gen flagfly = V162125x  
replace flagfly = . if flagfly < 0
replace flagfly = abs(flagfly - 7)
label var flagfly "6 = flag makes R feel extremely good; . . . ; 0 = extremely bad"
gen usabetter = V162123  
replace usabetter = . if usabetter < 0
replace usabetter = abs(usabetter - 5)
label var usabetter "4 = R agrees strongly the world would be better if more like USA; . . . ; 0 = "
gen ameridentity = V162332
replace ameridentity = . if ameridentity < 0
replace ameridentity = abs(ameridentity - 5)
label var ameridentity "4 = American identity is very important; . . . ; 0 = not at all important"
factor ameridentity usabetter flagfly, pcf
predict famerican
label var famerican "Factor score for ameridentity, flagfly, and usabetter (Eigenvalue = 1.71)"
gen partyid = V161158x
replace partyid = . if partyid < 0
replace partyid = partyid - 1
label var partyid "Partisan identification: 6 = strong Republican; . . . ; 0 = strong Democrat"
gen libconpre = V161126
replace libconpre = . if libconpre < 0 
recode libconpre 99 = .
replace libconpre = libconpre - 1
label var libconpre "Political ideology without forced choice: 6 = strong conservative; . . . ;"
gen flibcon = V161127
replace flibcon = . if flibcon < 0
recode flibcon 1 = 0 2 = 2 3 = 1
label var flibcon "Forced choice political ideology for moderates"
gen libcon = libconpre
recode libcon . = 2 if flibcon == 0
recode libcon . = 4 if flibcon == 1
recode libcon . = 3 if flibcon == 2
label var libcon "Political ideology (forced): 6 = strong conservative; . . . ; 0 = strong liberal"
gen madjust = V162207
replace madjust = . if madjust < 0
replace madjust = abs(madjust - 5)
label var madjust "We should adjust morality to changing world (4 = strongly agree)"
gen mbreakdown = V162208  
replace mbreakdown = . if mbreakdown < 0
replace mbreakdown = abs(mbreakdown - 5)
label var mbreakdown  "Newer lifestyles contributing to breakdown of society (4 = strongly agree)"
gen mtolerant = V162209
replace mtolerant = . if mtolerant < 0
replace mtolerant = abs(mtolerant - 5)
label var mtolerant "We should be tolerant of those with different morality (4 = strongly agree)"
gen mtraditional = V162210  
replace mtraditional = . if mtraditional < 0
replace mtraditional = abs(mtraditional - 5)
label var mtraditional "Country better with emphasis on traditional family ties (4 = strongly agree)"
factor madjust mbreakdown mtolerant mtraditional, pcf
predict fmoral
label var fmoral "Factor score for four moral traditionalism items (Eigenvalue = 2.16)"
gen bspecial = V162211
replace bspecial = . if bspecial < 0
replace bspecial = abs(bspecial - 5)
label var bspecial " Blacks should work way up without special favors (4 = strongly agree)"
gen bdifficult = V162212
replace bdifficult = . if bdifficult < 0
replace bdifficult = abs(bdifficult - 5)
label var bdifficult "Slavery and discrimination prevent blacks from improving (4 = strongly agree}"
gen bless = V162213
replace bless = . if bless < 0
replace bless = abs(bless - 5)
label var bless "Over the years blacks have gotten less than they deserve (4 = strongly agree)"
gen btryharder = V162214
replace btryharder = . if btryharder < 0
replace btryharder = abs(btryharder - 5)
label var btryharder "Blacks could be better off if they just tried harder (4 = strongly agree)"
factor bspecial bdifficult bless btryharder, pcf
predict fblack
gen fracial = -1 * fblack
label var fracial "Racial resentment scale: -1 * fblack"
gen authind = V162239
replace authind = . if authind < 0
recode authind 2 = 2 1 = 0 3 = 1
label var authind "2 = R prefers respect for elders; . . . ; 0 = R prefers independence"
gen authmanners = V162240
replace authmanners = . if authmanners < 0
recode authmanners 2 = 2 1 = 0 3 = 1
label var authmanners "2 = R prefers good manners; . .. ; 0 = R prefers curiosity"
gen authobed = V162241
replace authobed = . if authobed < 0
recode authobed 2 = 0 1 = 2 3 = 1
label var authobed "2 = R prefers obedience; . . . ; 0 = R prefers self-reliance"
gen authwbehaved = V162242
replace authwbehaved = . if authwbehaved < 0
recode authwbehaved 2 = 2 1 = 0 3 = 1
label var authwbehaved "2 = R prefers well-behaved; . . . ; 0 = R prefers considerate"
factor authind authmanners authobed authwbehaved, pcf
predict fauthoritarian
label var fauthoritarian "Factor score for four authoritarianism (Eigenvalue = 1.94)"
gen pforborn = V161315
replace pforborn = . if pforborn < 0
replace pforborn = pforborn - 1
label var pforborn "Number of parents born outside of the United States"
gen forborn = 0
recode forborn 0 = 1 if V161316 == 4
label var forborn "1 = R was born in a foreign country; 0 = otherwise"
gen gforborn = V161317
replace gforborn = . if gforborn < 0
label var gforborn "Number of grandparents born outside of the United States"
gen educ1 = V161270
replace educ1 = . if educ1 < 0
gen educ = .
recode educ . = 0 if educ1 < 5
recode educ . = 1 if educ1 > 4 & educ1 < 9
recode educ . = 2 if educ1 == 9
recode educ . = 3 if educ1 == 10
recode educ . = 4 if educ1 == 11
recode educ . = 4 if educ1 == 12
recode educ . = 5 if educ1 == 13
recode educ . = 6 if educ1 == 14
recode educ . = 7 if educ1 == 15
recode educ . = 7 if educ1 == 16
label var educ "0 = less than high school; . . .; 7 = doctorate or professional degree"
gen hhincome = V161361x
replace hhincome = . if hhincome < 1
replace hhincome = hhincome - 1
label var hhincome  "27 = highest income category; . . . ; 0 = lowest income category"
gen gender = V161342
recode gender -9 = . 3 = . 1 = 0 2 = 1
label var gender "1 = R is a woman; 0 = R is a man"
gen race = V161310x
replace race = . if race < 0
label var race"1 = white; 2 = black; 3 = Asian; 4 = Native American; 5 = Hispanic; 6 = other"
gen white = 0
recode white 0 = 1 if race == 1
label var white "1 = R is white non=Hispanic; 0 = otherwise"
gen black = 0
recode black 0 = 1 if race == 2
label var black "1 = R is black non=Hispanic; 0 = otherwise"
gen asian = 0
recode asian 0 = 1 if race == 3
label var asian "1 = R is Asian non=Hispanic; 0 = otherwise"
gen natamer = 0
recode natamer 0 = 1 if race == 4
label var natamer "1 = R is Native American non=Hispanic; 0 = otherwise"
gen hispanic = V161309
recode hispanic -9 = . -8 = . 2 = 0
label var hispanic  "1 = R is Hispanic; 0 = otherwise"
gen otherrace = 0
recode otherrace 0 = 1 if race == 6
label var otherrace "1 = R is other or mixed race; 0 = otherwise"
gen age = V161267
replace age = . if age < 0
label var age "R age (in years)"
gen chattend = V161245
replace chattend = . if chattend < 0
recode chattend  1 = 4 2 = 3 3 = 2 4 = 1 5 = 0 
replace chattend = 0 if V161244 == 2
label var chattend "4 = attend services every week; . . . ; 0 = never"
gen hhunion = V161302
replace hhunion = . if hhunion < 0
recode hhunion 2 = 0
label var hhunion "1 = R is member of household with at least one union member; 0 = otherwise"
gen weight = V160102 
ologit immculture famerican partyid libcon fmoral fracial fauthoritarian  forborn pforborn gforborn educ hhincome gender black hispanic asian age [iweight = weight]
ologit immcrime famerican partyid libcon fmoral fracial fauthoritarian  forborn pforborn gforborn educ hhincome gender black hispanic asian age [iweight = weight]
ologit immbadecon famerican partyid libcon fmoral fracial fauthoritarian  forborn pforborn gforborn educ hhincome gender black hispanic asian age [iweight = weight]
regress fimmthreat famerican partyid libcon fmoral fracial fauthoritarian  forborn pforborn gforborn educ hhincome gender black hispanic asian age [iweight = weight]
margins, at(famerican = (-3.74 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0 0.5 1.0 1.46)) atmeans noatlegend
ologit pvote16 fimmthreat famerican partyid libcon forborn pforborn gforborn educ hhincome gender black hispanic asian age chattend hhunion [iweight = weight]
margins, at(fimmthreat = (-1.57 -1.0 -0.5 0 0.5 1.0 1.5 2.0 2.52)) atmeans noatlegend
ologit pvote16 famerican fimmthreat c.famerican#c.fimmthreat partyid libcon forborn pforborn gforborn educ hhincome gender black hispanic asian age chattend hhunion [iweight = weight]
margins, at(fimmthreat = (-1.568058 -1.0 -0.5 0 0.5 1.0 1.5 2.0 2.522859) famerican = (-1)) atmeans noatlegend
margins, at(fimmthreat = (-1.568058 -1.0 -0.5 0 0.5 1.0 1.5 2.0 2.522859) famerican = (1)) atmeans noatlegend
margins, at(famerican = (-3.739 -3 -2.5 -2 -1.5 -1.0 -0.5 0 0.5 1.0 1.46) fimmthreat = (-1)) atmeans noatlegend
margins, at(famerican = (-3.739 -3 -2.5 -2 -1.5 -1.0 -0.5 0 0.5 1.0 1.46) fimmthreat = (1)) atmeans noatlegend